<template>
  <div>
    <van-icon
      :color="collected ? '#ffe000' : '#777'"
      :name="collected ? 'star' : 'star-o'"
      @click="onCollected"
    />
  </div>
</template>

<script>
import { addArticleCollected, deleteArticleCollected } from '@/api/article.js'
export default {
  name: 'articleCollected',
  data () {
    return {

    }
  },
  props: {
    // 收藏状态
    collected: {
      type: Boolean,
      required: true
    },
    // 文章id
    artId: {
      type: [Number, Object],
      required: true
    }
  },
  // 修改v-model规则
  model: {
    prop: 'collected'
  },
  methods: {
    // 点击 =>收藏 取消收藏
    async onCollected () {
      try {
        // 取消收藏
        if (this.collected) {
          await deleteArticleCollected(this.artId)
          this.$toast.success('取消收藏成功')
        } else {
          // 收藏
          await addArticleCollected(this.artId)
          this.$toast.success('收藏成功')
        }
        // 更改视图
        this.$emit('input', !this.collected)
      } catch (err) {
        this.$toast.success('操作失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
</style>
